package com.yc.booktable.web;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.booktable.entity.AdminUser;
import com.yc.booktable.entity.Food;
import com.yc.booktable.mapper.AdminUserMapper;
import com.yc.booktable.mapper.FoodMapper;
import com.yc.booktable.service.impl.AdminUserServiceImpl;
import com.yc.booktable.vo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class IndexAction {
    @Resource
    private AdminUserMapper adminUserMapper;
    @Resource
    private AdminUserServiceImpl adminUserService;
    @Resource
    private FoodMapper foodMapper;

    @RequestMapping("/admin")
    public String admin(HttpSession session, Model model){
        AdminUser user = (AdminUser) session.getAttribute("loginedUser");
        if(user==null || user.getState()==1){
            return "redirect:/login";
        }
        model.addAttribute("user",user);
        return "admin";
    }
    @RequestMapping("loginout")
    public String logout(HttpSession session){
        session.invalidate();
        return "index";
    }
    /*@RequestMapping("/user")
    public String user(){
        return "user";
    }*/
/*    @RequestMapping("/table")
    public String table(){
        return "table";
    }*/
//    @RequestMapping("/typography")
//    public String typography(){
//        return "typography";
//    }
//    @RequestMapping("/icons")
//    public String icons(){
//        return "icons";
//    }
//    @RequestMapping("/maps")
//    public String maps(){
//        return "maps";
//    }
//    @RequestMapping("/notifications")
//    public String notifications(){
//        return "notifications";
//    }
    @RequestMapping("/dashboard")
    public String dashboard(HttpSession session,Model model){
        AdminUser user = (AdminUser) session.getAttribute("loginedUser");
        if(user==null){
            return "redirect:/login";
        }
        model.addAttribute("user",user);
        return "dashboard";
    }

    /**
     * 字段查询
     * @param id
     * @param account
     * @param name
     * @return
     */
    @PostMapping("/upgrade/query.s")
    @ResponseBody
    public Result queryBy(Integer id, Integer account, String name){
        QueryWrapper<AdminUser> queryWrapper = new QueryWrapper<>();
        if(account!=null ){
            queryWrapper.eq("account",account);
        }
        if(name!=null && !"".equals(name)){
            queryWrapper.eq("name",name);
        }
        if(id!=null){
            queryWrapper.eq("id",id);
        }
        List<AdminUser> list = adminUserMapper.selectList(queryWrapper);
        if(list != null){
            return new Result(1,"获取成功",list);
        }else{
            return new Result(0,"获取失败",null);
        }
    }
    @PostMapping("/upgrade/queryf.s")
    @ResponseBody
    public Result queryfBy(Integer fid, String ftype, String fname){
        QueryWrapper<Food> queryWrapper = new QueryWrapper<>();
        if(fid!=null ){
            queryWrapper.eq("fid",fid);
        }
        if(ftype!=null && !"".equals(ftype)){
            queryWrapper.eq("ftype",ftype);
        }
        if(fname!=null && !"".equals(fname)){
            queryWrapper.eq("fname",fname);
        }
        List<Food> list = foodMapper.selectList(queryWrapper);
        if(list != null){
            return new Result(1,"获取成功",list);
        }else{
            return new Result(0,"获取失败",null);
        }
    }
    @RequestMapping("/upgrade")
    public String upgrade(HttpSession session, Model model,@RequestParam(defaultValue = "1",value = "pageNum") Integer PageNum){
        AdminUser user = (AdminUser) session.getAttribute("loginedUser");
        if(user==null|| user.getState()==1){
            return "redirect:/login";
        }
        model.addAttribute("user",user);
       /* model.addAttribute("adminuser1",new AdminUser());
        Page page = new Page(PageNum,10);
        page = adminUserMapper.selectPage( page,null);
        // List<AdminUser> adminUsers = adminUserMapper.selectList(null);
        model.addAttribute("adminuser",page);*/
        return "upgrade";
    }
}
